package com.tz.controller;

import com.tz.entity.Research;
import com.tz.entity.json.ResearchJson;
import com.tz.service.ResearchsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 于大海 on 2018/1/6.
 */
@Controller
@RequestMapping("/statistics")
public class ResearchsController {
    @Autowired
    ResearchsService researchsService;

    @ResponseBody
    @RequestMapping(value = "/month",produces = "application/json")

    public Map<String ,ResearchJson> getMonth(@RequestBody String date) {

        Map<String, ResearchJson> researchJsonMap = new HashMap<>();


        Map<String, List<Research>> map = researchsService.findMonth(date);
        System.out.println(map);
        for (String key : map.keySet()) {
            ResearchJson researchJson = new ResearchJson();
            Double avg = 0.0;
            System.out.println(key);
            List<Research> value = map.get(key);
            System.out.println("11"+value.size());

            for (Research research : value) {
                avg += research.getAver();
            }
            avg = avg / (value.size());
            researchJson.setAvg(avg);
            researchJson.setResearchs(value);
            researchJsonMap.put((String) key, researchJson);
        }
        return  researchJsonMap;
    }
    @ResponseBody
    @RequestMapping(value = "/quarter",produces = "application/json")
    public Map<String ,ResearchJson> getQuarter(@RequestBody String date) {
        System.out.println("111hhhh"+date);
        String[] strArray = null;

        strArray = date.split(",");
        strArray[1]=strArray[1]+"-"+"30";
        Map<String, ResearchJson> researchJsonMap = new HashMap<>();
        Map<String, List<Research>> map = researchsService.findQuarter(strArray[0],strArray[1]);
        System.out.println(map);
        for (String key : map.keySet()) {
            ResearchJson researchJson = new ResearchJson();
            Double avg = 0.0;
            List<Research> value = map.get(key);
            for (Research research : value) {
                avg += research.getAver();
            }
            avg = avg / (value.size());

            researchJson.setAvg(avg);
            researchJson.setResearchs(value);
            researchJsonMap.put((String) key, researchJson);
        }
        return  researchJsonMap;
    }


    @ResponseBody
    @RequestMapping(value = "/year",produces = "application/json")
    public Map<String ,ResearchJson> getYear(@RequestBody String date) {
        Map<String, ResearchJson> researchJsonMap = new HashMap<>();
        Map<String, List<Research>> map = researchsService.findYear(date);
        for (String key : map.keySet()) {
            ResearchJson researchJson = new ResearchJson();
            Double avg = 0.0;
            List<Research> value = map.get(key);
            for (Research research : value) {
                avg += research.getAver();
            }
            avg = avg / (value.size());
            researchJson.setAvg(avg);
            researchJson.setResearchs(value);
            researchJsonMap.put((String) key, researchJson);
        }
        return  researchJsonMap;
    }

}